2.20.3 [5] <2.8> Show the contents of the stack after each function call, assuming
that the input is 4.
For the following problems, the table has an assembly code fragment that computes
a Fibonacci number. However, the entries in the table have errors, and you will be
asked to fix these errors. For number n, the Fibonacci of n is calculated as follows:
n fibonacci of n
1 1
2 1
3 2
4 3
5 5
6 8
7 13
8 21
a. FIB: addi $sp, $sp, –12
sw $ra, 0($sp)
sw $s1, 4($sp)
sw $a0, 8($sp)
slti $t0, $a0, 1
beq $t0, $0, L1
addi $v0, $a0, $0
j EXIT
L1: addi $a0, $a0, –1
jal FIB
addi $s1, $v0, $0
addi $a0, $a0, –1
jal FIB
add $v0, $v0, $s1
EXIT: lw $ra, 0($sp)
lw $a0, 8($sp)
lw $s1, 4($sp)
addi $sp, $sp, 12
jr $ra
b. FIB: addi $sp, $sp, -12
sw $ra, 8($sp)
sw $s1, 4($sp)
sw $a0, 0($sp)
slti $t0, $a0, 3
beq $t0, $0, L1
addi $v0, $0, 1
j EXIT
L1: addi $a0, $a0, -1
jal FIB
addi $a0, $a0, -2
jal FIB
add $v0, $v0, $s1
EXIT: lw $a0, 0($sp)
lw $s1, 4($sp)
lw $ra, 8($sp)
addi $sp, $sp, 12
jr $ra
 
 
View Solution
 
 
 
<< Back Next >>